<template>
  <div>
    <div class="content-header" style="padding-bottom: 5px">
      <h1>套餐管理</h1>
      <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>预约管理</el-breadcrumb-item>
        <el-breadcrumb-item>套餐管理</el-breadcrumb-item>
      </el-breadcrumb>
    </div>

    <div>
      <div>
        <div class="filter-container" style="float: left;padding-bottom: 5px">
          <el-input placeholder="编码/名称/助记码" v-model="pageNation.queryString" style="width: 200px;padding-right: 10px" class="filter-item" @keyup.enter.native="handleFilter"></el-input>
          <el-button @click="findPage" class="dalfBut">查询</el-button>
          <el-button type="primary"  @click="handleCreate()">新建</el-button>
        </div>
        <!--数据展示区域-->
        <div>
          <el-table :data="setMealList" border style="width: 100%"  >
            <el-table-column type="index" align="center" label="序号" width="50"></el-table-column>
            <el-table-column prop="code" label="项目编码" align="center" width="80"></el-table-column>
            <el-table-column prop="name" label="名称" align="center" width="200"></el-table-column>
            <el-table-column prop="helpCode" label="助记码" align="center" width="90"></el-table-column>
            <el-table-column prop="sex" label="适用性别" align="center" width="80">
              <template slot-scope="scope">
                <span>{{scope.row.sex == '0' ? '不限' : scope.row.sex == '1' ? '男' : '女'}}</span>
              </template>
            </el-table-column>
            <el-table-column prop="age" label="适用年龄" align="center" width="90"></el-table-column>
            <el-table-column prop="price" label="套餐价格" align="center" width="90"></el-table-column>
            <el-table-column prop="remark" label="项目说明" align="center" ></el-table-column>
            <el-table-column prop="attention" label="注意事项" align="center" width="80"></el-table-column>
            <el-table-column   label="图片" align="center" width="150">
              <template slot-scope="scope">
              <img :src="'http://localhost:8814/img/'+scope.row.img" alt="null" height="150px" width="150px">
              </template>
            </el-table-column>
            <el-table-column prop="id" label="详情" align="center" width="250">
              <template slot-scope="scope">
                <el-button type="primary" size="small" @click="handleShowSetMeal(scope.row.id)">详情</el-button>
                <el-button type="primary" size="small" @click="handleSetMealEdit(scope.row)">编辑</el-button>
                <el-button type="danger" size="small" @click="handleDeleteSetMeal(scope.row.id)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
          <div class="block">
            <el-pagination
                background
                @current-change="handleCurrentPage"
                @size-change="handleSizeChange"
                :current-page="pageNation.currentPage"
                :page-size="pageNation.pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :page-sizes="[5, 10, 20, 30]"
                :total="pageNation.total">
            </el-pagination>
          </div>
        </div>
      </div>
      <!--*****弹出框****-->
      <!--新建套餐对话框-->
      <div>
        <el-dialog title="新建套餐对话框" :visible.sync="dialogFormVisible4SetMealAdd">
          <el-form ref="dataUpdateForm"   label-position="right" label-width="100px">
            <el-row>
              <el-col :span="12">
                <el-form-item label="项目编码" prop="code">
                  <el-input v-model="addSetMealData.code" style="width: 220px"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="项目名称" prop="name">
                  <el-input v-model="addSetMealData.name" ></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="项目名称" prop="helpCode">
                  <el-input v-model="addSetMealData.helpCode" ></el-input>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="12">
                <el-form-item label="性别">
                  <el-select v-model="addSetMealData.sex">
                    <el-option label="不限" value="0"></el-option>
                    <el-option label="男" value="1"></el-option>
                    <el-option label="女" value="2"></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="适用年龄">
                  <el-input v-model="addSetMealData.age"/>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="价格">
                  <el-input v-model="addSetMealData.price"/>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="项目说明">
                  <el-input v-model="addSetMealData.remark"/>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="注意事项">
                  <el-input v-model="addSetMealData.attention"/>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="24">
                <el-form-item label="上传图片">
                  <el-upload
                      class="avatar-uploader"
                      action="/setMeal/upload"
                      :auto-upload="autoUpload"
                      name="imgFile"
                      :show-file-list="false"
                      :on-success="handleAvatarSuccess"
                      >
                    <img v-if="imageUrl" :src="imageUrl" class="avatar" height="150px" width="150px">
                    <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                  </el-upload>
                </el-form-item>
              </el-col>
            </el-row>
            <div class="checkScrol">
              <table class="datatable">
                <thead>
                <tr>
                  <th>选择</th>
                  <th>项目编码</th>
                  <th>项目名称</th>
                  <th>项目说明</th>
                </tr>
                </thead>
                <tbody>
                <tr v-for="c in allCheckGroupData">
                  <td>
                    <input :id="c.id" v-model="checkGroupIds4Add" type="checkbox" :value="c.id">
                  </td>
                  <td><label :for="c.id">{{c.code}}</label></td>
                  <td><label :for="c.id">{{c.name}}</label></td>
                  <td><label :for="c.id">{{c.remark}}</label></td>
                </tr>
                </tbody>
              </table>
            </div>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="cancelSetMealAdd">取 消</el-button>
            <el-button type="primary" @click="SetMealAdd">确 定</el-button>
          </div>
        </el-dialog>
      </div>
      <!--查看检查项详情对话框-->
      <div>
        <el-dialog title="查看套餐对话框" :visible.sync="dialogFormVisible4CheckGroupShow">
          <el-table :data="checkGroupData" border  >
            <el-table-column type="index" align="center" label="序号" ></el-table-column>
            <el-table-column prop="code" label="项目编码" align="center" ></el-table-column>
            <el-table-column prop="name" label="名称" align="center" ></el-table-column>
            <el-table-column prop="sex" label="适用性别" align="center" >
              <template slot-scope="scope">
                <span>{{scope.row.sex == '0' ? '不限' : scope.row.sex == '1' ? '男' : '女'}}</span>
              </template>
            </el-table-column>
            <el-table-column prop="helpCode" label="助记码" align="center" ></el-table-column>
            <el-table-column prop="price" label="价格" align="center" ></el-table-column>
            <el-table-column prop="remark" label="项目说明" align="center" ></el-table-column>
            <el-table-column prop="attention" label="注意事项" align="center" ></el-table-column>
            <div slot="footer" class="dialog-footer">
              <el-button @click="dialogFormVisible4CheckGroup = false">关闭</el-button>
            </div>
          </el-table>
        </el-dialog>
      </div>

      <!--编辑套餐对话框-->
      <div>
        <el-dialog title="编辑套餐对话框" :visible.sync="dialogFormVisible4SetMealEdit">
          <el-form ref="dataUpdateForm"   label-position="right" label-width="100px">
            <el-row>
              <el-col :span="12">
                <el-form-item label="项目编码" prop="code">
                  <el-input v-model="setMealData.code" style="width: 220px"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="项目名称" prop="name">
                  <el-input v-model="setMealData.name" ></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="项目名称" prop="helpCode">
                  <el-input v-model="setMealData.helpCode" ></el-input>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="12">
                <el-form-item label="性别">
                  <el-select v-model="setMealData.sex">
                    <el-option label="不限" value="0"></el-option>
                    <el-option label="男" value="1"></el-option>
                    <el-option label="女" value="2"></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="适用年龄">
                  <el-input v-model="setMealData.age"/>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="价格">
                  <el-input v-model="setMealData.price"/>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="项目说明">
                  <el-input v-model="setMealData.remark"/>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="注意事项">
                  <el-input v-model="setMealData.attention"/>
                </el-form-item>
              </el-col>
              <el-col>
                <el-form-item label="已经绑定的图片展示">
                  <template slot-scope="scope">
                    <img :src="'http://localhost:8814/img/'+setMealData.img" alt="null" height="150px" width="150px">
                  </template>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="24">
                <el-form-item label="上传图片">
                  <el-upload
                      class="avatar-uploader"
                      action="/setMeal/upload"
                      :auto-upload="autoUpload"
                      name="imgFile"
                      :show-file-list="false"
                      :on-success="handleAvatarSuccess"
                  >
                    <img v-if="imageUrl" :src="setMealData.img" class="avatar" height="150px" width="150px">
                    <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                  </el-upload>
                </el-form-item>
              </el-col>
            </el-row>
            <div class="checkScrol">
              <table class="datatable">
                <thead>
                <tr>
                  <th>选择</th>
                  <th>项目编码</th>
                  <th>项目名称</th>
                  <th>项目说明</th>
                </tr>
                </thead>
                <tbody>
                <tr v-for="c in allCheckGroupData">
                  <td>
                    <input :id="c.id" v-model="checkGroupIds4Edit" type="checkbox" :value="c.id">
                  </td>
                  <td><label :for="c.id">{{c.code}}</label></td>
                  <td><label :for="c.id">{{c.name}}</label></td>
                  <td><label :for="c.id">{{c.remark}}</label></td>
                </tr>
                </tbody>
              </table>
            </div>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="cancelSetMealEdit">取 消</el-button>
            <el-button type="primary" @click="SetMealEdit">确 定</el-button>
          </div>
        </el-dialog>
      </div>

    </div>

  </div>
</template>

<script>
import {getRequest} from "../../utils/api";
import {postRequest} from "../../utils/api";
import {putRequest} from "../../utils/api";
import {deleteRequest} from "../../utils/api";

export default {
  name: "setMeal",
  data(){
    return{
      dialogFormVisible4CheckGroupShow: false,
      dialogFormVisible4SetMealEdit:false,
      dialogFormVisible4SetMealAdd:false,
      autoUpload:true,
      imageUrl:null,
      pageNation:{
        currentPage: 1,
        pageSize:5,
        queryString: '',
        total: 0,
      },
      setMealList:[],
      setMealData:{
      },
      addSetMealData:{
        code:'',
        name:'',
        sex:'',
        helpCode:'',
        price:'',
        age:'',
        remark:'',
        attention:'',
        img:null,
      },
      allCheckGroupData:[],
      checkGroupData:[],
      checkGroupIds4Edit:[],
      checkGroupIds4Add:[],

    }
  },
  methods:{
    handleAvatarSuccess(response,file){
      message:response.message;
      if(response.status == 200){
        //预览刚刚上传的文件
        this.imageUrl = "http://localhost:8814/img/"+response.obj;
        this.addSetMealData.img =response.obj;
        this.setMealData.img = response.obj;
        // this.formData.img = response.data;//将文件名称赋值给模型数据，用于表单提交
      }
    },
    cancelSetMealAdd(){
      this.addSetMealData = {};
      this.imageUrl = '';
      this.dialogFormVisible4SetMealAdd = false;
    },
    cancelSetMealEdit(){
      this.setMealData = [];
      this.checkGroupIds = [];
      this.dialogFormVisible4SetMealEdit = false;
    },
    handleShowSetMeal(id){
      getRequest("/setMeal/group/"+id).then(res=>{
        this.checkGroupData = res;
      })
      this.dialogFormVisible4CheckGroupShow = true;
    },
    findCheckGroupIdsBySetMealId(id){
      getRequest("/setMeal/getGroupIdsById/"+id).then(res=>{
        if (res){
          this.checkGroupIds4Edit = res;
        }
      })
    },
    findAllCheckGroup(){
      getRequest("/setMeal/group/").then(res=>{
        if(res){
        this.allCheckGroupData = res;
        }
      })
    },
    handleSetMealEdit(setMeal){
      this.setMealData = setMeal;
      this.findAllCheckGroup();
      this.findCheckGroupIdsBySetMealId(setMeal.id);
      this.dialogFormVisible4SetMealEdit = true;
    },
    SetMealAdd(){
      let ids = this.checkGroupIds4Add;
      let sm = this.addSetMealData;
      let url = "/setMeal/?";
      for (let i = 0; i < ids.length; i++) {
        if(i == ids.length-1){
          url += "items="+ids[i];
          break;
        }
        url += "items="+ids[i]+"&";
      }
      url = url+"&code="+sm.code+"&name="+sm.name+"&sex="+sm.sex+"&helpCode="+sm.helpCode+"&price="+sm.price
            +"&age="+sm.age+"&remark="+sm.remark+"&attention="+sm.attention+"&img="+sm.img;

       // console.log("url="+url);
      postRequest(url).then(res=>{
        if(res){
          this.findPage();
          this.dialogFormVisible4SetMealAdd = false;
          this.checkGroupIds4Add = [];
          this.addSetMealData = {};
        }
      })
    },
    SetMealEdit(){
      let ids = this.checkGroupIds4Edit;
      let sm = this.setMealData;
      let url = "/setMeal/?";
      for (let i = 0; i < ids.length; i++) {
        if(i == ids.length-1){
          url += "items="+ids[i];
          break;
        }
        url += "items="+ids[i]+"&";
      }
      url = url+"&id="+sm.id+"&code="+sm.code+"&name="+sm.name+"&sex="+sm.sex+"&helpCode="+sm.helpCode+"&price="+sm.price
          +"&age="+sm.age+"&remark="+sm.remark+"&attention="+sm.attention+"&img="+sm.img;

      // console.log("url="+url);
      putRequest(url).then(res=>{
        if(res){
          this.findPage();
          this.checkGroupIds4Edit = [];
          this.setMealData = {};
          this.dialogFormVisible4SetMealEdit = false;
        }
      })

    },
    handleDeleteSetMeal(id){
      //弹出确认框
      this.$confirm('你确定要删除当前数据吗？','提示',{
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        //发送ajax请求，将id提交到Controller
        deleteRequest("/setMeal/" +id).then((res) => {
          if(res.status ==200){
            this.findPage();
            this.$message({
              type:'success',
              message:res.message,
            });
            this.findByCondition();
          }else{
            //执行失败
            this.$message.error(res.message);
          }
        }).catch((r) => {
          this.showMessage(r);
        });
      }).catch(() => {
        this.$message("已取消");
      });
    },
    handleCreate(){
      this.findAllCheckGroup();
      this.dialogFormVisible4SetMealAdd = true;
    },
    findPage(){
      getRequest("/setMeal/",this.pageNation).then(res=>{
        if(res){
          this.pageNation.total = res.total;
          this.setMealList = res.data;
        }
      })
    },
    handleSizeChange(pageSize){
      this.pageNation.pageSize = pageSize;
      this.findPage();
    },
    handleCurrentPage(currentPage){
      this.pageNation.currentPage = currentPage;
      this.findPage();
    },
  },
  created() {
    this.findPage();
  }
}
</script>

<style scoped>

</style>